package de.wittje.games.mlpbd.controller.listeners;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import de.wittje.games.mlpbd.controller.SoundController;
import de.wittje.games.mlpbd.view.panels.TitlePanel;

public class TitleListener implements KeyListener {

	private SoundController sc;
	private TitlePanel screen;
	private long lastMoved = System.currentTimeMillis();
	private ArrayList<Integer> keys = new ArrayList<Integer>();
	private boolean inWork = false;

	public TitleListener(TitlePanel screen) {
		sc = new SoundController();
		this.screen = screen;
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent e) {

		if (!inWork) {
			if (!keys.contains(e.getKeyCode())) {
				keys.add(e.getKeyCode());
				calc();
			}
		}

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	private void calc() {
		inWork = true;
		for (int i : keys) {
			if (i == KeyEvent.VK_DOWN) {
				lastMoved = System.currentTimeMillis();
				screen.moveCursorDown();
				screen.repaint();
			} else if (i == KeyEvent.VK_UP) {
				lastMoved = System.currentTimeMillis();
				screen.moveCursorUp();
				screen.repaint();
			} else if (i == KeyEvent.VK_SPACE) {
				screen.select();
			}
		}
		keys.clear();
		inWork = false;
	}

}
